package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.impl.UserDaoImpl;
import entity.User;

public class Login_Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String error="请确认您的账号和密码!";
		String forwardPath="/login.jsp";
		UserDaoImpl u=new UserDaoImpl();
		User user =u.selectUser(username);
		if(u.findUser(username, password)){
			Cookie c = new Cookie("username",username);
			c.setPath("/"); 
			response.addCookie(c);
			request.getSession().setAttribute("user", user);
			forwardPath="/MianPagelistServlet";	
		}else{
			request.getSession().setAttribute("error", error);
		}
		response.sendRedirect(request.getContextPath()+forwardPath);
	}

}
